﻿
var mntPanelRepository = new function () {
	this.maintenanceOn = new Image();
	this.maintenanceOn.src = "imgs/mechanicalPanel/mnt.png";
	this.maintenanceOff = new Image();
	this.maintenanceOff.src = "imgs/mechanicalPanel/mntDisable.png";
	this.minor = new Image();
	this.minor.src = "imgs/mechanicalPanel/malMinor.png";
	this.moderate = new Image();
	this.moderate.src = "imgs/mechanicalPanel/malModerate.png";
	this.severe = new Image();
	this.severe.src = "imgs/mechanicalPanel/malSevere.png";
	this.malfunctionDisable = new Image();
	this.malfunctionDisable.src = "imgs/mechanicalPanel/malDisable.png";

	this.mntCanvas = document.getElementById("maintenanceButtonPanelCanvas");
	this.rprCanvas = document.getElementById("repairButtonPanelCanvas");
	if (this.mntCanvas.getContext) {
		this.mntContex = this.mntCanvas.getContext('2d');
		this.rprContex = this.rprCanvas.getContext('2d');
	}
};
function MaintenanceController() {

	this.background;
	this.x0 = 15;
	this.y0 = 10;

	this.init = function () {
		this.mntCtx = mntPanelRepository.mntContex;
		this.setBackGround(mntPanelRepository.maintenanceOn);
		this.mntCtx.drawImage(this.background,0 ,0 );
	};

	this.changed = true;
	this.draw = function () {
		if (this.changed == true) {
			this.mntCtx.clearRect(0, 0, 300, 300);
			this.mntCtx.drawImage(this.background, this.x0 + 30, this.y0 + 30, 140, 140);

			this.mntCtx.lineWidth = 5;
			this.mntCtx.strokeStyle = "White";
			this.mntCtx.strokeText("Maintenance", this.x0 + 30, this.y0 + 100);

			this.mntCtx.shadowColor = "White";
			this.mntCtx.shadowBlur = 10;
			this.mntCtx.shadowOffsetX = 2;
			this.mntCtx.shadowOffsetY = 2;
			this.mntCtx.fillStyle = "Black";
			this.mntCtx.font = "bold 24px Arial";
			this.mntCtx.fillText("Maintenance", this.x0 + 30, this.y0 + 100);
			this.mntCtx.fillText("5$", this.x0 + 75, this.y0 + 160);


			this.changed = false;
		}

	};

	this.setBackGround = function (image){
		this.changed = true;
		this.background = image;
	};

	this.disable = function () {
		this.setBackGround(mntPanelRepository.maintenanceOff);
		$("#buttonMechanicalPanel").attr('disabled', 'disabled');
		$("#buttonMechanicalPanel").css('cursor', 'default');
	};

	this.onMalfunctionOccour = function(){
		//this.disable();
	};

	this.onFreeze = function(){
		this.disable();
	};

	this.onStopFreeze = function (mechanialCondition) {
		this.setBackGround(mntPanelRepository.maintenanceOn);
		$("#buttonMechanicalPanel").removeAttr('disabled');   
		$("#buttonMechanicalPanel").css('cursor', 'pointer');
	};
}

function RepairController() {

	this.isHide = true;
	this.isFreeze = false;
	this.background;
	this.cost;
	this.x0 = 15;
	this.y0 = 10;

	this.init = function () {
		this.rprCtx = mntPanelRepository.rprContex;
		this.hide();
	};

	this.draw = function () {
		if (this.changed == true) {
			this.rprCtx.clearRect(0, 0, 300, 300);

			if (this.isHide == false) {
				this.rprCtx.drawImage(this.background, this.x0 + 5, this.y0 + 5, 165, 140);

				this.rprCtx.textAlign = "center";
				this.rprCtx.shadowColor = "White";
				this.rprCtx.shadowBlur = 10;
				this.rprCtx.shadowOffsetX = 2;
				this.rprCtx.shadowOffsetY = 2;
				this.rprCtx.fillStyle = "Black";
				this.rprCtx.font = "bold 24px Arial";
				this.rprCtx.fillText("Repair", this.x0 + 90, this.y0 + 85);


				this.rprCtx.lineWidth = 5;
				this.rprCtx.strokeStyle = "White";
				this.rprCtx.strokeText("Repair", this.x0 + 90, this.y0 + 85);

				this.rprCtx.shadowColor = "White";
				this.rprCtx.shadowBlur = 10;
				this.rprCtx.shadowOffsetX = 2;
				this.rprCtx.shadowOffsetY = 2;
				this.rprCtx.fillStyle = "Black";
				this.rprCtx.font = "bold 24px Arial";
				this.rprCtx.fillText("Repair", this.x0 + 90, this.y0 + 85);
				this.rprCtx.fillText(this.cost, this.x0 + 90, this.y0 + 160);
			}

			this.changed = false;
		}

	};

	this.setMechanicalCondition = function (mechanicalCondition) {
		if (mechanicalCondition == enumMC.FIX) {
			return;
		}
		this.show();
		this.cost = getNewScenario().getCostTable().getRepairCost(mechanicalCondition) + "$";
		if (this.isFreeze == false) {
			switch (mechanicalCondition) {
			case enumMC.MINOR: this.setBackGround(mntPanelRepository.minor); break;
			case enumMC.MODERATE: this.setBackGround(mntPanelRepository.moderate); break;
			case enumMC.SEVERE: this.setBackGround(mntPanelRepository.severe); break;
			}
		}
	};

	this.setBackGround = function (image) {
		this.changed = true;
		this.background = image;
	};

	this.onMalfunctionRepair = function () {
		this.hide();
	};

	this.onStopFreeze = function (mechanicalCondition) {
		this.isFreeze = false;
		if (mechanicalCondition == enumMC.FIX) {
			this.hide();
		} else {
			this.setMechanicalCondition(mechanicalCondition);
			$("#buttonRepairMechanicalPanel").removeAttr('disabled');
			$("#buttonRepairMechanicalPanel").css('cursor', 'pointer');
		}
	};

	this.onFreeze = function (mechanicalCondition) {
		this.isFreeze = true;
		if (mechanicalCondition == enumMC.FIX) {
			return;
		}
		this.setBackGround(mntPanelRepository.malfunctionDisable);
		$("#buttonRepairMechanicalPanel").attr('disabled', 'disabled');
		$("#buttonRepairMechanicalPanel").css('cursor', 'default');
	};

	this.show = function () {
		$("#buttonRepairMechanicalPanel").show();
		this.isHide = false;
		this.changed = true;
	};

	this.hide = function () {
		$("#buttonRepairMechanicalPanel").hide();
		this.isHide = true;
		this.changed = true;
	};

}
var msgPanelRepository = new function () {

	console.log("panelRepository created");

	this.iconFuel = new Image();
	this.iconFuel.src = "imgs/panel/msgIcon_fuel.png";
	this.iconMaintenance = new Image();
	this.iconMaintenance.src = "imgs/panel/msgIcon_mnt.png";
	this.iconMinor = new Image();
	this.iconMinor.src = "imgs/panel/nsgIcon_malMinor.png";
	this.iconModerate = new Image();
	this.iconModerate.src = "imgs/panel/nsgIcon_malModerate.png";
	this.iconSevere = new Image();
	this.iconSevere.src = "imgs/panel/nsgIcon_malSevere.png";


	this.msgPanelCanvas = document.getElementById("messagesPanelCanvas");
	if (this.msgPanelCanvas.getContext) {
		this.msgPanelContext = this.msgPanelCanvas.getContext('2d');
	}
};

function MessageController() {

	this.msg1;
	this.msg2;
	this.nextMessageIndex = 0;

	this.addMessage = function (msgController) {
		msgController.clear();

		if (this.msg1 == undefined || this.msg1.isAlive == false) {
			msgController.y0 = 0;
			this.msg1 = msgController;
			this.nextMessageIndex = 1;
		} else {
			if (this.nextMessageIndex == 0) {
				this.msg1 = msgController;
			} else {
				this.msg2 = msgController;
			}
			msgController.y0 = this.nextMessageIndex;
			this.nextMessageIndex = (this.nextMessageIndex + 1) % 2;
		}

		msgController.run();
	};

	function MassagePanelDrawable(backgroundImg) {

		this.backgroung = backgroundImg;
		this.isAlive = false;
		this.endTime = 0;
		this.context = msgPanelRepository.msgPanelContext;
		this.width = msgPanelRepository.msgPanelCanvas.width;
		this.height = msgPanelRepository.msgPanelCanvas.height;
		this.height2 = this.height / 2;
		this.y0 = 0;

		this.nextFrameTime;
		this.frameFresh = 200;

		this.init = function (text, durationInMsec, displayTimeLeft) {
			this.startTime = Date.now();
			this.nextFrameTime = this.startTime;
			this.endTime = this.startTime + durationInMsec;
			this.text = text;
			this.isAlive = true;
			this.displayTimeLeft = displayTimeLeft;
		};

		this.run = function () { };

		this.draw = function () {
			if (this.isAlive) {
				currentDate = Date.now();
				if (currentDate < this.endTime) {
					if (this.nextFrameTime < currentDate){
						//console.log("currentDate: " + currentDate);
						this.nextFrameTime = currentDate + 200;
						this.context.save();
						this.context.clearRect(0, this.y0 * this.height2, this.width, this.height2);
						this.context.drawImage(this.backgroung, 0, this.y0 * this.height2, 70, 70);
						this.context.restore();

						var roundedTime = 0;
						if (this.displayTimeLeft) {
							dt = this.endTime - Date.now();
							reminder = dt % 1000;
							roundedTime = (dt - reminder) / 1000;
						}
						this.drawText(this.context, roundedTime + 1, this.displayTimeLeft);
					}
					return true;
				}
				this.clear();
				this.isAlive = false;
			}

			return false;
		};



		this.drawText = function (context, leftTimeInsec, displayTimeLeft) {
			var textToDisplay = this.text;
			if (displayTimeLeft) {
				textToDisplay += " (" + leftTimeInsec + " seconds)";
			}
			this.context.save();
			this.context.shadowColor = this.getTextShadowColor();
			this.context.shadowBlur = 10;
			this.context.shadowOffsetX = 2;
			this.context.shadowOffsetY = 2;
			this.context.font = this.getFontSize() + " Arial";
			this.context.fillText(textToDisplay, 85, 50 + this.y0 * this.height2);
			this.context.shadowOffsetX = -2;
			this.context.shadowOffsetY = -2;
			this.context.font = this.getFontSize() + " Arial";
			this.context.fillText(textToDisplay, 85, 50 + this.y0 * this.height2);
			this.context.restore();
		};

		this.getTextShadowColor = function () {return 'rgb(100,100,100)';};
		this.getFontSize = function () {return "30px";};
		this.clear = function () {
			this.context.clearRect(0, 0, this.width, this.height);
			this.isAlive = false;
		};
	}

	RefuelMessageController = function () {
		this.getTextShadowColor = function () { return 'rgb(0,255,255)'; };
		this.onRefueling = function (refillTime) {
			this.run = function () {
				this.init("Refueling...", refillTime, true);
			};
		};
	};
	RefuelMessageController.prototype = new MassagePanelDrawable(msgPanelRepository.iconFuel);
	this.refuelMessageController = new RefuelMessageController();

	MaintenanceMessageController = function () {

		this.getTextShadowColor = function () { return 'rgb(0,255,0)'; };

		this.onMaintenanceDone = function (timeMsec) {
			this.run = function () {
				this.init("Maintenance in progress…", timeMsec, true);
			};
		};
	};
	MaintenanceMessageController.prototype = new MassagePanelDrawable(msgPanelRepository.iconMaintenance);
	this.maintenanceMessageController = new MaintenanceMessageController();

	MalfunctionMessageController = function () {

		this.mc = 1;
		this.onOccur = function (mechanicalCondition) {
			this.mc = mechanicalCondition;
			switch (this.mc) {
			case enumMC.MINOR: this.backgroung = msgPanelRepository.iconMinor; break;
			case enumMC.MODERATE: this.backgroung = msgPanelRepository.iconModerate; break;
			case enumMC.SEVERE: this.backgroung = msgPanelRepository.iconSevere; break;
			}
		};

		this.getOccourString = function (mechanicalCondition) {
			switch (mechanicalCondition) {
			case enumMC.MINOR: return "Minor Malfunction!";
			case enumMC.MODERATE: return "Moderate Malfunction!!";
			case enumMC.SEVERE: return "Severe Malfunction!!!";
			}
		};

		this.getRepairString = function (mechanicalCondition) {
			switch (mechanicalCondition) {
			case enumMC.MINOR: return "Repairing a minor malfunction…";
			case enumMC.MODERATE: return "Repairing a moderate malfunction…";
			case enumMC.SEVERE: return "Repairing a severe malfunction…";
			}
		};

		this.getTextShadowColor = function () {
			switch (this.mc) {
			case enumMC.MINOR: return 'rgb(255,255,0)';
			case enumMC.MODERATE: return 'rgb(200,50,0)';
			case enumMC.SEVERE: return 'rgb(255,0,0)';
			}
		};
	};
	MalfunctionMessageController.prototype = new MassagePanelDrawable(msgPanelRepository.iconMinor);
	this.malfunctionMessageController = new MalfunctionMessageController();

	this.onMaintenanceDone = function (timeMsec) {
		this.maintenanceMessageController.onMaintenanceDone(timeMsec);
		this.addMessage(this.maintenanceMessageController);
	};

	this.onMaintenanceAlert = function (timeMsec) {
		this.maintenanceMessageController.run = function () {
			this.init("Maintenance recommended (press Enter)", timeMsec, false);
		};
		this.addMessage(this.maintenanceMessageController);
	};

	this.onRefueling = function(refillTime){
		//msg = new RefuelMessageController();
		//msg.onStationRefueling();
		this.refuelMessageController.onRefueling(refillTime);
		this.addMessage(this.refuelMessageController);
	};

	this.onWaitForFeulShip = function (timeinMSec) {
		this.refuelMessageController.run = function () {
			this.init("Wait for fuel ship", timeinMSec, true);
		};
		this.addMessage(this.refuelMessageController);
	};

	this.onMalfunctionOccur = function (mechanicalCondition) {
	    this.maintenanceMessageController.clear();
		this.malfunctionMessageController.onOccur(mechanicalCondition);
		this.malfunctionMessageController.run = function () {
			this.init(this.getOccourString(mechanicalCondition), 3000, false);
			this.getFontSize = function () { return "30px"; };
		};
		this.addMessage(this.malfunctionMessageController);
	};

	this.onMalfunctionRepair = function (mechanicalCondition) {
		this.malfunctionMessageController.run = function () {
			this.init(this.getRepairString(mechanicalCondition), getNewScenario().getCostTable().getRepairTime(mechanicalCondition), true);
			this.getFontSize = function () { return "25px"; };
		};
		this.addMessage(this.malfunctionMessageController);
	};

	this.onFreezeDueToSeverMalfunction = function(){
		this.malfunctionMessageController.run = function () {
			this.init("Ship is frozen. Please repair the malfunction.", 360 * 1000, false);
			this.getFontSize = function () { return "29px"; };
		};
		this.addMessage(this.malfunctionMessageController);
	};
	this.draw = function () {
		if (this.msg1 != undefined && this.msg1.isAlive) {
			this.msg1.draw();
		}
		if (this.msg2 != undefined && this.msg2.isAlive) {
			this.msg2.draw();
		}
		//this.maintenanceMessageController.draw();
		//this.refuelMessageController.draw();
	};
}

//General controller 
function PanelContoller() {

	this.msgController = new MessageController();
	this.mntController = new MaintenanceController();
	this.repairController = new RepairController();

	this.init = function () {
		console.log("panelController init");
		this.mntController.init();
		this.repairController.init();
	};

	this.onMaintenanceDone = function (timeMsec) {
		this.msgController.onMaintenanceDone(timeMsec);
	};

	this.onMaintenanceAlert = function(timeMsec){
		this.msgController.onMaintenanceAlert(timeMsec);
	};

	this.onRefueling = function (timeInSec) {
		this.msgController.onRefueling(timeInSec);
	};

	this.onWaitForFeulShip = function (time) {
		this.msgController.onWaitForFeulShip(time);
	};
	this.tempIndex = 0;
	this.onMalfunctionOccurTest = function () {
		this.tempIndex = (this.tempIndex + 1) % 3;
		this.onMalfunctionOccur(2 + this.tempIndex);
		this.repairController.setMechanicalCondition(2 + this.tempIndex);
	};

	this.onMalfunctionOccur = function (mechanialCondition) {
		//remove maintenance messages.
		//this.msgController.maintenanceMessageController.clear();

		this.msgController.onMalfunctionOccur(mechanialCondition);
		this.mntController.onMalfunctionOccour();
		this.repairController.setMechanicalCondition(mechanialCondition);
	};



	this.onMalfunctionRepairTest = function(){
		this.msgController.onMalfunctionRepair(2+ this.tempIndex);
	};

	this.onMalfunctionRepair = function(mechanialCondition) {
		//remove maintenance messages.
		//this.msgController.maintenanceMessageController.clear();
		this.msgController.onMalfunctionRepair(mechanialCondition);
		this.repairController.onMalfunctionRepair();
	};

	this.onFreeze = function () {
		this.mntController.onFreeze();
		this.repairController.onFreeze(getCurrentMechanicalCondition());
	};

	this.onStopFreeze = function () {
		this.mntController.onStopFreeze(getCurrentMechanicalCondition());
		this.repairController.onStopFreeze(getCurrentMechanicalCondition());
	};

	this.onFreezeDueToSeverMalfunction = function () {
		this.msgController.onFreezeDueToSeverMalfunction();
	};

	this.draw = function(timeInMsec) {
		this.msgController.draw();
		this.mntController.draw();
		this.repairController.draw();
	};
}

var panelController = new PanelContoller();
window.panelController.init();


